<?php
/**
 * Created by PhpStorm.
 * User: suruixiang
 * Date: 2017/9/29
 * Time: 下午4:19
 */
namespace app\admin\model;

use think\Model;
use think\Config;

class User extends Model
{
    /**
     * 根据单字段获取信息
     * @param $user_name
     * @param string $fields
     * @return object
     */
    public function getInfoByField($field_name, $field_value, $fields = '*')
    {
        return $this->where($field_name, $field_value)->field($fields)->find();
    }

    /**
     * 获取用户列表
     * @return array
     */
    public function getAllLists(array $where = [])
    {
        $status = [
            1 => '正常',
            2 => '待审核',
            3 => '审核拒绝',
            4 => '禁用'
        ];
        $lists = $this->where($where)->order('id desc')->paginate(Config::get('config.page_num'), false, ['query'=>request()->param()]);
        $lists_arrsy = [];
        foreach ($lists as $li){
            $lists_arrsy[] = [
                'id'          => $li->id,
                'nick_name'   => $li->nick_name,
                'user_name'   => $li->user_name,
                'mobile'      => $li->mobile,
                'head_img'    => $li->head_img,
                'create_time' => $li->create_time,
                'audit_time'  => $li->audit_time ? date('Y-m-d H:i:s', $li->audit_time) : '',
                'cumulative_earning' => $li->cumulative_earning,
                'cumulative_traffic' => $li->cumulative_traffic,
                'status'      => $status[$li->status],
                'status_code' => $li->status,
            ];
        }
        return [
            'lists' => $lists_arrsy,
            'page'  => $lists->render()
        ];
    }

    /**
     * 删除用户
     * @param $user_id
     * @return int
     */
    public function userDel($user_id)
    {
        return $this->where('id', $user_id)->update([
            'is_del' => 1
        ]);
    }

    /**
     * 修改用户状态
     * @param $user_id
     * @param int $status
     * @param array $where
     * @param array $expand
     * @return $this
     */
    public function userUpStatus($user_id, $status = 1, array $where = [], array $expand = [])
    {
        $where['id'] = $user_id;
        $expand['status'] = $status;
        return $this->where($where)->update($expand);
    }
}